import WxRequest from './core/WxRequest'
import GlobalData from '../../globalData'

// https: //github.com/wux-weapp/wx-extend/blob/master/docs/components/request.md
// const app = getApp() 
const header = () => {
  const token = wx.getStorageSync('token')
  if (token) {
    return {
      'Authorization': token
    }
  }
  return {}
}

const Ajax = new WxRequest({
  baseURL: GlobalData.baseUrl,
  header: {},
  // `transformRequest` 允许在向服务器发送前，转换请求数据
  transformRequest: [
    (data, header) => {
      // console.log(data, 'transformRequest');
      return data
    },
  ],
  // `transformResponse` 转换响应数据
  transformResponse: [
    (data, header) => {
      if (typeof data === 'string') {
        try {
          data = JSON.parse(data)
        } catch (e) {
          /* Ignore */
        }
      }
      return data
    },
  ],

  // 基于响应状态返回成功或失败
  // validateStatus: status => status >= 200 && status < 300,
})

Ajax.interceptors.use({
  // 请求数据
  request(request) {
    wx.showLoading({
      title: '加载中...',
    })
    return request
  },
  // 请求失败
  requestError(requestError) {
    console.error(requestError, 'requestError');
    wx.hideLoading()
    return Promise.reject(requestError)
  },
  // 响应数据
  response(response) {
    wx.hideLoading()
    const _code = [200, 10]
    if (!_code.includes(response.data.code)) {
      wx.showToast({
        title: response.data.msg || "出现错误",
        icon: 'error'
      })
      console.error(response.data.msg);
    } else {
      // const {
      //   version
      // } = response.data
      // const appVersion = app.globalData.version
      // // const v = Object.values(app.globalData.version)
      // // v = v.filter(item => item)
      // if (version.dict != appVersion.dict || version.dept != appVersion.dept || version.team != appVersion.team || version.user != appVersion.user) {
      //   app.initSystem()
      //   app.globalData.version = version
      // }


    }
    return response
  },
  // 响应失败
  responseError(responseError) {
    wx.hideLoading()
    return Promise.reject(responseError)
  },
})

// 如果你想在稍后移除拦截器
// this.interceptors = Ajax.interceptors.use(obj)
// Ajax.interceptors.eject(this.interceptors)

export const HTTP_GET = (url, params, callback, isAddRouterPath = true) => {
  url = isAddRouterPath ? `${GlobalData.requestUrlPath}${url}` : url
  Ajax.defaults.header = header()
  return Ajax.getRequest(url, {
    data: params
  }).then((res) => {
    callback(res.data)
  })
}
export const HTTP_POST = (url, params, callback, isAddRouterPath = true) => {
  url = isAddRouterPath ? `${GlobalData.requestUrlPath}${url}` : url
  Ajax.defaults.header = header()

  Ajax.postRequest(url, {
    data: params
  }).then((res) => {
    callback(res.data)
  })
}
export const HTTP_PUT = (url, params, callback, isAddRouterPath = true) => {
  url = isAddRouterPath ? `${GlobalData.requestUrlPath}${url}` : url
  Ajax.defaults.header = header()

  return Ajax.putRequest(url, {
    data: params
  }).then((res) => {
    callback(res.data)
  })
}
export const HTTP_DELETE = (url, params, isAddRouterPath = true) => {
  url = isAddRouterPath ? `${GlobalData.requestUrlPath}${url}` : url
  Ajax.defaults.header = header()

  return Ajax.deleteRequest(url, {
    data: params
  })
}

// this.WxRequest.headRequest(url, config)
// this.WxRequest.optionsRequest(url, config)
// this.WxRequest.traceRequest(url, config)
// this.WxRequest.connectRequest(url, config)
// export default WxRequest